﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Handlers.ProjectActualResourceHandler;
using ETL.Business.Handlers.ProjectUserHandler;
using ETL.Entity;

namespace ETL.Business.Models
{
    public class ProjectTaskModel
    {
        public TASK ProjectTasks { get; set; }

        /// <summary>
        /// Domain model constructor
        /// </summary>
        /// <param name="projectTasks"></param>
        public ProjectTaskModel(TASK projectTasks)
        {
            this.ProjectTasks = projectTasks;
        }

        /// <summary>
        /// Get task id
        /// </summary>
        public decimal TaskId { get { return Convert.ToInt16(this.ProjectTasks.TASK_ID); } }

        /// <summary>
        /// Get gen task id
        /// </summary>
        public decimal GenTaskId { get { return Convert.ToInt16(this.ProjectTasks.GEN_TASK_ID); } }

        /// <summary>
        /// Get project id
        /// </summary>
        public decimal ProjectId { get { return Convert.ToInt16(this.ProjectTasks.GEN_PROJECT_ID); } }

        /// <summary>
        /// Get task description
        /// </summary>
        public string ProjectName { get { return this.ProjectTasks.PROJECT_NAME; } }

        /// <summary>
        /// Get task description
        /// </summary>
        public string TaskName {
            get { return this.ProjectTasks.TASK_NAME; }
            set { ProjectTasks.TASK_NAME = value; }
        }

        /// <summary>
        /// Get task description
        /// </summary>
        public decimal? PercentComplete{ get;set; }

        /// <summary>
        /// Calculate total time of a task
        /// </summary>
        public string TotalTime
        {
            get
            {
                string str = string.Empty;
                var total = new TimeSpan(0);
                var allActualResourceUsage = (new GetActualResourceUsage()).Execute(ProjectId, TaskId);
                if (allActualResourceUsage != null && allActualResourceUsage.Count() > 0)
                {
                    var lstActualResourceUsage = allActualResourceUsage.ToList();
                    foreach (var projectActualResourceUsageModel in lstActualResourceUsage)
                    {
                        if (projectActualResourceUsageModel.StartDate != null && projectActualResourceUsageModel.EndDate != null)
                        {
                            TimeSpan? result = projectActualResourceUsageModel.EndDate - projectActualResourceUsageModel.StartDate;
                            if (result.HasValue)
                            {
                                total += result.Value;

                            }
                        }
                    }
                }
                str = total.ToString();
                return str;
            }
        }

        public List<ProjectUserModel> AssignedUsers
        {
            get
            {
                var result = (new GetUserOfTask()).Execute(TaskId.ToString());
                return result;
            }

        }

        public List<ProjectUserModel> UnassignedUsers
        {
            get
            {
                var assignedUsers = AssignedUsers;
                var allUsers = AllUsers;
                var tmpUnassignedUsers = new List<ProjectUserModel>();
                foreach (var allUser in allUsers)
                {
                    bool isContain = false;
                    foreach (var assignedUser in assignedUsers)
                    {
                        if(allUser.Id == assignedUser.Id)
                        {
                            isContain = true;
                            break;
                        }
                    }

                    if(!isContain)
                    {
                        tmpUnassignedUsers.Add(allUser);
                    }
                }
                return tmpUnassignedUsers;
            }

        }
        public List<ProjectUserModel> AllUsers
        {
            get
            {
                var result = (new GetActiveUsers()).ExecuteToList();

                return result;
            }
        }
        public string DisplayStartDate
        {
            get
            {
                var scheduledResourceUsage = ProjectTasks.SCHEDULED_RESOURCE_USAGES.FirstOrDefault();
                if(scheduledResourceUsage != null)
                {
                    var firstStartDate = scheduledResourceUsage.START_DATE;
                    if(firstStartDate.HasValue)
                    {
                        return firstStartDate.Value.ToString("MM/dd/yyyy");
                    }
                    
                }
                return string.Empty;
            }
        }

        public string DisplayEndDate
        {
            get
            {
                var scheduledResourceUsage = ProjectTasks.SCHEDULED_RESOURCE_USAGES.FirstOrDefault();
                if (scheduledResourceUsage != null)
                {
                    var firstEndDate = scheduledResourceUsage.END_DATE;
                    if (firstEndDate.HasValue)
                    {
                        return firstEndDate.Value.ToString("MM/dd/yyyy");
                    }

                }
                return string.Empty;
            }
        }

        public string DisplayStartTime
        {
            get
            {
                var scheduledResourceUsage = ProjectTasks.SCHEDULED_RESOURCE_USAGES.FirstOrDefault();
                if (scheduledResourceUsage != null)
                {
                    var firstStartDate = scheduledResourceUsage.START_DATE;
                    if (firstStartDate.HasValue)
                    {
                        return firstStartDate.Value.ToString("hh:mm tt");
                    }

                }
                return string.Empty;
            }
        }

        public string DisplayEndTime
        {
            get
            {
                var scheduledResourceUsage = ProjectTasks.SCHEDULED_RESOURCE_USAGES.FirstOrDefault();
                if (scheduledResourceUsage != null)
                {
                    var firstEndDate = scheduledResourceUsage.END_DATE;
                    if (firstEndDate.HasValue)
                    {
                        return firstEndDate.Value.ToString("hh:mm tt");
                    }

                }
                return string.Empty;
            }
        }
    }
}
